//
//  webViewController.m
//  maocuhuiOASystem
//
//  Created by michael on 16/4/6.
//  Copyright © 2016年 czd. All rights reserved.
//
#import <QuickLook/QuickLook.h>

#import "webViewController.h"

@interface webViewController ()<QLPreviewControllerDataSource,QLPreviewControllerDelegate>

@end

@implementation webViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户指南";
    self.view.backgroundColor = [UIColor whiteColor];
    _webview.backgroundColor = [UIColor whiteColor];
    _webview = [[UIWebView alloc]initWithFrame:self.view.frame];
       _webview.delegate=self;
        _webview.scrollView.bounces = NO;

      _webview.scalesPageToFit = YES;
      [self.view addSubview:_webview];


   [self requstdata];
}
- (void)requstdata{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"userId":userId}];
    [HttpTools requestWithURL:KExchmain arrays:array method:@"helpDoc" success:^(id JSON) {
    NSLog(@"-------%@",JSON);
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSArray *array = JSON[@"data"];
NSDictionary *dic = [array firstObject];
_attacmentURL = dic[@"downlink"];
 NSURL *urlstr = [NSURL URLWithString:_attacmentURL];
        NSURLRequest *request = [NSURLRequest requestWithURL:urlstr];
    
    // 3. 发送请求给服务器
    [self.webview loadRequest:request];
//        [self openFileURL:_attacmentURL];
 fujianwebViewController *vc = [[fujianwebViewController alloc]init];
//        vc.url = _attacmentURL;
//        [self.navigationController pushViewController:vc animated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
      
    }];
}
//-(void)openFileURL:(NSString*)url{
//    NSString * stringurl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    self.attacmentURL =  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[url lastPathComponent]]; //附件路径
// 
//    if ([self isExtistFilePathWithFileName:[[NSURL URLWithString:stringurl] lastPathComponent]]) { //已经存在附件
//        QLPreviewController *previewoCntroller = [[QLPreviewController alloc] init] ;
//        previewoCntroller.delegate=self;
//        previewoCntroller.dataSource=self;
//        previewoCntroller.navigationItem.rightBarButtonItem=nil;
//        [self.navigationController pushViewController: previewoCntroller animated:YES];
//
//    }else{
//        MBProgressHUD * hud  =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//        hud.labelText = @"下载中...";
//        NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringurl]];
//        [self.webview loadRequest:request];
//    }
//    
//}
//
//#pragma mark PreviewDataSource
////第一个方法是设定一组文档浏览的个数，会影响到在文档预览中的上/下一页。
//-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
//    return 1;
//}
////第二个方法是指定文档预览的文件路径
//-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
//    
//    return [NSURL fileURLWithPath:self.attacmentURL];
//}
//
//-(BOOL)isExtistFilePathWithFileName:(NSString*)fileName{
//    NSString * file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    NSString * filePath = [file stringByAppendingPathComponent:fileName];
//    NSFileManager * fileManager = [NSFileManager defaultManager];
//    if ([fileManager fileExistsAtPath:filePath] && fileName.length != 0) {
//        return YES;
//    }else{
//        return NO;
//    }
//    
//}
//
//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//    NSURL * requestedURL = [request URL];
//    NSData * fileData = [[NSData alloc] initWithContentsOfURL:requestedURL];
//    NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//     [fileData writeToFile:[filePath stringByAppendingPathComponent:[requestedURL lastPathComponent]] atomically:YES];
//      return YES;
//}
//- (void)webViewDidStartLoad:(UIWebView *)webView{
//    
//    
//}
//- (void)webViewDidFinishLoad:(UIWebView *)webView{
//    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
//    QLPreviewController *previewoCntroller = [[QLPreviewController alloc] init] ;
//    previewoCntroller.delegate=self;
//    previewoCntroller.dataSource=self;
//    [self.navigationController pushViewController: previewoCntroller animated:YES];
//}
//- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
//    
//    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
